package com.runchinaup.sifitbledome.activity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.runchinaup.sifitbledome.R;
import com.runchinaup.sifitbledome.domain.BleData;

import java.util.ArrayList;

/**
 * Created by liber on 2018/3/15.
 */

public abstract class ScanAdapter extends BaseAdapter {

    private Context context;
    ArrayList<BleData> data;

    public ScanAdapter(Context context, ArrayList<BleData> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View ret = null;

        if (convertView != null) {
            ret = convertView;
        } else {
            ret = LayoutInflater.from(context).inflate(R.layout.item_scan, parent, false);
        }

        ViewHolder viewHolder = (ViewHolder) ret.getTag();
        if (viewHolder == null) {
            viewHolder = new ViewHolder();
            viewHolder.name = ret.findViewById(R.id.name);
            viewHolder.adress = ret.findViewById(R.id.adress);
            viewHolder.layout = ret.findViewById(R.id.scan_lay);
            ret.setTag(viewHolder);
        }

        final BleData bleData = data.get(position);

        viewHolder.name.setText(bleData.getName());
        viewHolder.adress.setText(bleData.getAdress());

        viewHolder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onLayoutClick(bleData);
            }
        });


        return ret;
    }

    protected abstract void onLayoutClick(BleData bleData);

    private static class ViewHolder {
        public TextView name;
        public TextView adress;
        public RelativeLayout layout;
    }


}
